package com.rambo.dsc.controller;

import com.rambo.dsc.handler.WsClientAuthHandler;
import com.rambo.dsc.wsclient.user.UserPO;
import com.rambo.dsc.wsclient.user.UserPOArray;
import com.rambo.dsc.wsclient.user.UserWs;
import com.rambo.dsc.wsclient.user.UserWsImplService;
import com.rambo.tools.SecurityUtil;
import com.rambo.tools.StringUtil;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.wss4j.dom.handler.WSHandlerConstants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Create by Rambo on 2017/6/21
 **/
@RestController
public class CXFNoSpringController {
    @Value("${cxf.server.url}")
    private String cxfServerUrl;

    @Value("${cxf.ws.user}")
    private String cxfWsUser;

    @Value("${cxf.ws.pwd.type}")
    private String cxfWsPwsType;

    @RequestMapping("/cxf/nospring")
    public Map<String, Object> nospring() {
        Map<String, Object> map = new HashMap<>();
        try {
            UserWsImplService userWsImplService = new UserWsImplService(new URL(cxfServerUrl + "/UserWs?wsdl"));
            UserWs userWs = userWsImplService.getUserWsImplPort();
            addWSS4JOutInterceptor(userWs);

            UserPO user = userWs.getUser("031e7a36972e11e6acede16e8241c0fe");
            map.put("1.获取单个用户:", user);

            user.setPhone("18975468245");
            UserPO userPO1 = userWs.updateUser(user);
            map.put("2.更新单个用户:", userPO1);

            UserPO userPO2 = new UserPO();
            userPO2.setUuid(StringUtil.getUUID());
            userPO2.setName("rambo");
            userPO2.setPasswd(SecurityUtil.encryptMD5("123456"));
            userPO2.setSex("男");
            userPO2.setYxbz("Y");
            UserPO userPO3 = userWs.addUser(userPO2);
            map.put("3.新增单个用户:", userPO3);

            UserPO userPO4 = new UserPO();
            userPO4.setSex("男");
            UserPOArray userPOArray1 = userWs.listUser(userPO4);
            map.put("4.获取所有的男用户:", userPOArray1);

            UserPO userPO5 = new UserPO();
            userPO5.setSex("男");
            UserPOArray userPOArray2 = userWs.listUserOrdrBy(userPO5, "sorts", true);
            map.put("5.获取所有的男用户并按照 sorts 字段排序:", userPOArray2.getItem());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return map;
    }

    private void addWSS4JOutInterceptor(Object wsClass) {
        Endpoint cxfEndpoint = ClientProxy.getClient(wsClass).getEndpoint();
        Map outProps = new HashMap();
        outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
        outProps.put(WSHandlerConstants.USER, cxfWsUser);
        outProps.put(WSHandlerConstants.MUST_UNDERSTAND, "0");
        outProps.put(WSHandlerConstants.PASSWORD_TYPE, cxfWsPwsType);
        outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, WsClientAuthHandler.class.getName());
        cxfEndpoint.getOutInterceptors().add(new WSS4JOutInterceptor(outProps));
    }
}